//
//  LGBaisController.m
//  微博oc版
//
//  Created by Apple_Lzzy27 on 16/10/10.
//  Copyright © 2016年 Apple_Lzzy27. All rights reserved.
//

#import "LGBaisController.h"
#import "LGVisitorView.h"
@interface LGBaisController ()<UITableViewDelegate, UITableViewDataSource>



@end

@implementation LGBaisController{
    NSString *_title;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self setNavBar];
    self.isLogin = NO;
  
    self.isLogin ? [self setTableView]:[self setVisitorViews];
    
}

//懒加载

- (LGVisitorView *)visitorView{
    if (_visitorView == nil) {
     
        _visitorView = [[LGVisitorView alloc] initWithFrame:self.view.bounds];
        NSLog(@"%@",_visitorView);
    }
    
    return _visitorView;
}

- (NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    
    return _dataArray;
}




- (UIRefreshControl *)refresh{
    if (_refresh == nil) {
        _refresh = [[UIRefreshControl alloc] init];
    }
    
    return _refresh;
}

- (UINavigationBar *)navBar{
    //创建自己的navBar
    if (_navBar == nil) {
   
     _navBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, self.navigationController.navigationBar.bounds.size.width, 64)];
        _navBar.barTintColor = [UIColor cz_colorWithHex:0xF6F6F6];
       //
    self.navItem = self.navItem;
    //把UINavigationItem添加到自己的navbar上
    _navBar.items = @[self.navItem];
        
    }
   

    return _navBar;
}
//懒加载navItem
- (UINavigationItem *)navItem{
    
    if (_navItem == nil) {
        _navItem = [[UINavigationItem alloc] init];
    }
    
    
    return _navItem;
}





/**
 *   从写系统title的set,get方法方法
 *
 */

#pragma mark: 从写系统title的set,get方法方法
- (NSString *)title{
    
    
    return _title;
}

- (void)setTitle:(NSString *)title{
    self.navItem.title = title;
    self.tabBarItem.title = title;

    _title = title;
    
}

- (void)setNavBar{
    //隐藏系统的导航栏
    self.navigationController.navigationBar.hidden = YES;
    
    //把navBar添加到VIew上,执行get方法
    [self.view addSubview:self.navBar];
    
}


#pragma mark:访客视图
- (void)setVisitorViews{

    
    [self.visitorView.logButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
     [self.visitorView.regiButton addTarget:self action:@selector(login) forControlEvents:UIControlEventTouchUpInside];
    self.visitorView.visitorDict = self.visitorDictionary;
    
   
    [self.view insertSubview:self.visitorView belowSubview:self.navBar];
    //添加登录注册按钮
    self.navItem.leftBarButtonItem = [UIBarButtonItem BarButtonCustButton:@"登录" fontSize:14 addTarget:self action:@selector(login) isBack:NO];
    self.navItem.rightBarButtonItem = [UIBarButtonItem BarButtonCustButton:@"注册" fontSize:14 addTarget:self action:@selector(regin) isBack:NO];
    
    
}
//登录事件发送通知
- (void)login{
   [[NSNotificationCenter defaultCenter] postNotificationName:UserLoginNitification object:nil];
}


//注册事件
-(void)regin{
    
    printf("通知注册");
    
}




#pragma  mark:数据加载,上啦刷新下来刷新机会调用这个方法

-(void)loadData{
    
    NSLog(@"加载数据");
    
    [self.refresh endRefreshing];
    
    
    
}

#pragma  mark:tableView表格数据源代理方法方法

-(void)setTableView{
   
    UITableView *tableView = [[UITableView alloc] init];
    tableView.frame = self.view.frame;
    tableView.delegate = self;
    tableView.dataSource = self;
    [tableView addSubview:self.refresh];
    [self.view insertSubview:tableView belowSubview:self.navBar];
    [self.refresh addTarget:self action:@selector(loadData) forControlEvents:UIControlEventValueChanged];
    self.automaticallyAdjustsScrollViewInsets = NO;
    tableView.contentInset = UIEdgeInsetsMake(self.navBar.bounds.size.height, 0, 44, 0);
    self.tableView = tableView;
//warning:
    [self loadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    return  [[UITableViewCell alloc] init];
    
    
}

//下拉刷新实现
- (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath{
    //总的组数
    NSInteger section = tableView.numberOfSections - 1;
    NSInteger row = indexPath.row;
    NSInteger sec = indexPath.section;
    
    NSInteger index = [tableView numberOfRowsInSection:section];
    
    if(row < 0 || sec < 0){
        return;
    }

    
    if ((index - 1) == indexPath.row && self.isFull != YES) {
        //执行下拉刷新
        self.isFull = YES;
        [self loadData];
        
    }
    
    
}





@end
